{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d15964f0-3391-4cda-8c2d-cfc831a09a1e",
   "metadata": {},
   "source": [
    "# Molecular Symmetry\n",
    "\n",
    "This notebook demonstrates the symmetry module for working with molecular symmetry tasks such as converting between reducible representations and irreducible representations, predicting IR and Raman vibrational modes, and predicting symmetry-adapted linear combinations (SALCs)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5c278e2d-bdc6-40f7-9594-aaf7a88a01c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "from chempy.symmetry.representations import *\n",
    "from chempy.symmetry.salcs import *\n",
    "\n",
    "import sympy"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b4c6526a-abca-46a9-9a3a-1a36770b0ecb",
   "metadata": {},
   "source": [
    "## Reducible & Irreducible Representations\n",
    "\n",
    "### Create a reducible representation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "id": "35cb115e-f029-408b-9551-1d6ff456ddbe",
   "metadata": {},
   "outputs": [],
   "source": [
    "# representation with all motions including vibrations, rotations, and translations\n",
    "water = Reducible([9, -1, 3, 1], 'c2v', all_motion=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "id": "40486a15-2aa2-4375-aa7e-75cc1ddfd033",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[9, -1, 3, 1]"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "water.gamma"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "id": "1d817777-8c9f-4df7-aac0-500eb073b2ca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'c2v'"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "water.group"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "id": "83e9e897-5a25-4da4-aa31-30f93f99e491",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "water.all_motion"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "id": "ffc76e27-e6b3-4b25-98f1-a4dc3e4d986a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "E 8C₃ 3C₂ 6S₄ 6σd\n"
     ]
    }
   ],
   "source": [
    "print_header('td')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "id": "a5ace25d-575e-434c-8908-1dee7cd1112f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[4, 1, 0, 0, 2]"
      ]
     },
     "execution_count": 135,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# representation with all motions including vibrations, rotations, and translations\n",
    "Ni = Reducible([4, 1, 0, 0, 2], 'td', all_motion=False)\n",
    "Ni.gamma"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a3084b9-a83c-4af1-a462-8cf0940d0f96",
   "metadata": {},
   "source": [
    "###  Create reducible representation using `from_atoms()`\n",
    "Create reducible representations based on the number of atoms that remain stationary during each symmetry operation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "id": "4046f862-a0bb-4701-84d4-c262c51386ec",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "E 2C₄ C₂ 2C₂′ 2C₂″ i 2S₄ σh 2σv 2σd\n"
     ]
    }
   ],
   "source": [
    "print_header('d4h')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "id": "ff39ddf3-a927-4641-812f-d66530df1263",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([15,  1, -1, -3, -1, -3, -1,  5,  3,  1])"
      ]
     },
     "execution_count": 137,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "PtCl4 = Reducible.from_atoms([5, 1, 1, 3, 1, 1, 1, 5, 3, 1], 'd4h')\n",
    "PtCl4.gamma"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e75c6051-841a-4b44-afa2-5d69b3f33f7d",
   "metadata": {},
   "source": [
    "### Combining irreducibles to form a reducible representation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "id": "60661c9c-faa1-42f0-b0f0-a4ff9c0a7b43",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "╭─────┬───┬────┬────┬─────╮\n",
      "│ C2v │ E │ C₂ │ σv │ σvʹ │\n",
      "├─────┼───┼────┼────┼─────┤\n",
      "│ A1  │ 1 │ 1  │ 1  │ 1   │\n",
      "├─────┼───┼────┼────┼─────┤\n",
      "│ A2  │ 1 │ 1  │ -1 │ -1  │\n",
      "├─────┼───┼────┼────┼─────┤\n",
      "│ B1  │ 1 │ -1 │ 1  │ -1  │\n",
      "├─────┼───┼────┼────┼─────┤\n",
      "│ B2  │ 1 │ -1 │ -1 │ 1   │\n",
      "╰─────┴───┴────┴────┴─────╯\n"
     ]
    }
   ],
   "source": [
    "print_table('c2v')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "id": "f02ba2f9-b362-4b0a-82f3-dd8ad3451c0e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 0, 0, 2])"
      ]
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# add together A1 + B2\n",
    "Reducible.from_irred([1, 0, 0, 1], 'c2v').gamma"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b17d96eb-1f43-4520-a77a-77779c4f34f2",
   "metadata": {},
   "source": [
    "### Decompose or reduce a reducible representation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "id": "f6ef7c42-3980-45b0-b011-902558ec85cf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 1, 3, 2])"
      ]
     },
     "execution_count": 140,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# reduce or decompose reducible representation\n",
    "water.decomp()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ab6c6fa2-4990-43e7-a006-64ed901898f4",
   "metadata": {},
   "source": [
    "## Predict vibrations and IR/Raman active modes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "id": "109ed2ad-3580-446b-bd2b-fa434997cbc2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 0, 1, 0])"
      ]
     },
     "execution_count": 141,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "water.vibe_modes()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "id": "fbc0bd46-9fb7-4b74-a049-03f5a09f1365",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'A1': 2, 'A2': 0, 'B1': 1, 'B2': 0}"
      ]
     },
     "execution_count": 142,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "water.vibe_modes(to_dict=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "id": "d7d5d589-b866-4457-98da-a8e728cc654c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'A1': 2, 'A2': 0, 'B1': 1, 'B2': 0}"
      ]
     },
     "execution_count": 143,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "water.ir_active(to_dict=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "id": "e1f38e69-dbb4-41e5-9fb0-b654cc264f99",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'A1': 2, 'A2': 0, 'B1': 1, 'B2': 0}"
      ]
     },
     "execution_count": 144,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "water.raman_active(to_dict=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "717f8cd8-7286-4bdc-b2e1-0964374f2f45",
   "metadata": {},
   "source": [
    "## Symmetry-adapted linear combinations\n",
    "\n",
    "### Projection operator method"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "id": "15006cf1-31a3-4b31-9f0d-7e177c8af6e3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2*a + 2*b + 2*c, 0, 2*a - b - c]"
      ]
     },
     "execution_count": 145,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# NH3 (C3v)\n",
    "a, b, c = sympy.symbols('a b c')\n",
    "\n",
    "projection = [a, b, c, a, b, c]\n",
    "calc_salcs_projection(projection, 'c3v')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "id": "2598b42a-80a4-4e6a-8049-587ff381c362",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'A1': 2*a + 2*b + 2*c, 'A2': 0, 'E': 2*a - b - c}"
      ]
     },
     "execution_count": 146,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "calc_salcs_projection(projection, 'c3v', to_dict=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "85567820-c8b1-4c8d-8f5f-835853a5e30e",
   "metadata": {},
   "source": [
    "### Symmetry functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "id": "c7ff26ce-b521-4c4b-af68-46d53326f520",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[a + b + c + d, 0, a - b + c - d, 0, 0, 0, 0, 0, 0, [a - c, b - d]]"
      ]
     },
     "execution_count": 147,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# [PtCl4]2- (D4h)\n",
    "a, b, c, d = sympy.symbols('a b c d')\n",
    "\n",
    "angles = [[1, 0, 0], [0, 1, 0], [-1, 0, 0], [0, -1, 0]]\n",
    "calc_salcs_func(angles, 'd4h', [a, b, c, d], mode='vector')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "id": "690c64b8-3ff0-4d7d-b53d-4d891bf04203",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'A1g': a + b + c + d,\n",
       " 'A2g': 0,\n",
       " 'B1g': a - b + c - d,\n",
       " 'B2g': 0,\n",
       " 'Eg': 0,\n",
       " 'A1u': 0,\n",
       " 'A2u': 0,\n",
       " 'B1u': 0,\n",
       " 'B2u': 0,\n",
       " 'Eu': [a - c, b - d]}"
      ]
     },
     "execution_count": 148,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "calc_salcs_func(angles, 'd4h', [a, b, c, d], mode='vector', to_dict=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "id": "8de1a53e-1053-43cb-b62b-d833ea5aa52f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'A1g': a + b + c + d,\n",
       " 'A2g': 0,\n",
       " 'B1g': a - b + c - d,\n",
       " 'B2g': 0,\n",
       " 'Eg': 0,\n",
       " 'A1u': 0,\n",
       " 'A2u': 0,\n",
       " 'B1u': 0,\n",
       " 'B2u': 0,\n",
       " 'Eu': [-a + c, -b + d]}"
      ]
     },
     "execution_count": 149,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# [PtCl4]2- (D4h)\n",
    "a, b, c, d = sympy.symbols('a b c d')\n",
    "\n",
    "angles = [[0, -90], [90, -90], [180, -90], [270, -90]]\n",
    "calc_salcs_func(angles, 'd4h', [a, b, c, d], mode='angle', to_dict=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "120f74af-7ef5-4a4a-927a-872ca7c1e987",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
